<?php

class GalactusViewPost extends GalactusView {

	function __construct($control) {
		GalactusView::__construct($control);
	}

	public function retrieveCSS( ) {
		return '
			.post-area {
				overflow: auto;
			}

			.post-items li:nth-child(n+1) {
				background-color: white;
				margin-left: 0px !important;
			}


			.post-header {
				border-bottom: 1px solid #ddd;
				display: block;
				padding: 5px 10px 5px 10px;
			}

			.post-title {
				float: left;
				font-weight: bold;
				word-wrap: break-word;
			}

			.post-date {
				color: #999;
				float: right;
			}
			
			.post-button {
				float: right;
				margin-left: 15px;
			}

			.post-content {
				padding: 5px 10px 5px 10px;
				text-align: justify;
				word-wrap: break-word;
			}
		';
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_post_show( ) {

				$(".post-area").resize(function( ) {
					$(this).height(HEIGHT - $(".post-area").position( ).top - 80);
				});
				$(".post-area").trigger("resize");

				$("#input-title, #input-content").keypress(function(e) {
					if(e.which == 13)
						$("#contentarea").load("'.URL.'POST/ADD", $(".params").serializeArray( ));
				});

				$(".post-button").click(function( ) {
					$("#contentarea").load("'.URL.'POST/REMOVE", { params: $(this).val( ) });
				});
			}
		';
	}

	public function show( ) {
		echo '
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="offset1 span4">
						<form method="post" action="'.URL.'POST/ADD" class="well">
							<div class="controls controls-row">
								<input type="text" maxlength="32" placeholder="Título" name="params[ ]" class="span12" />
							</div>
							<div class="controls controls-row">
								<textarea rows="10" maxlength="512" placeholder="Conteúdo" name="params[ ]" class="span12"></textarea>
							</div>
							<div class="controls controls-row">
								<button type="submit" class="btn btn-primary span12"><i class="icon-envelope icon-white"></i> Enviar Recado</button>
							</div>
						</form>
					</div>
					<div class="post-area span6 well">'.$this->retrievePosts(true).'</div>
				</div>
			</div>
			<script type="text/javascript">$(galactus_view_post_show( ));</script>
		';
	}

	public function retrievePosts($button = false) {
		$posts = $this->control->model->retrievePosts( );
		if(empty($posts))
			return '
				<div class="alert">
					<button type="button" class="close" data-dismiss="alert">&times;</button>
					<strong>Recados:</strong> Não há recados disponíveis!
				</div>
			';
		$content = "";
		for($i = 0; $i < count($posts); ++$i) {
			$date = new DateTime($posts[$i]["created_on"]);
			$content .= '
				<li class="thumbnail span12">
					<div class="post-header">
						<div class="post-title">'.$posts[$i]["title"].'</div>
						'.(($button)? '<button type="button" class="close post-button" value="'.$posts[$i]["id"].'">×</button>' : NULL).'
						<div class="post-date">'.$date->format("d/m/Y H\hi").'</div>
						<div class="clearfix"></div>
					</div>
					<div class="post-content">'.$posts[$i]["content"].'</div>
				</li>
			';			
		}
		return '<ul class="thumbnails post-items">'.$content.'</ul>';
	}

}
?>
